
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.FilterIndexReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Iterator;

public class TrackSearcher {

	  //static final File INDEX_DIR = new File("track_artist.index");
	  static final File INDEX_DIR = new File("track_album_artist.index");
	
	public static void main(String[] args) {
		try {	
			new TrackSearcher();
		} catch(Exception e)
		{
			System.out.println(e);						
		}
	}
	
	
	public TrackSearcher() 
		throws Exception 
	{
		   IndexReader reader = IndexReader.open(INDEX_DIR);
		   
		   String queries="";
		   String field="track";

		 //   if (normsField != null)
		 //     reader = new OneNormsReader(reader, normsField);

		    Searcher searcher = new IndexSearcher(reader);
		    Analyzer analyzer = new StandardAnalyzer();
		    

		    String fields[] = { "track", "artist", "album" };
		    MultiFieldQueryParser qparser = new MultiFieldQueryParser(fields, analyzer);
	        //QueryParser parser = new QueryParser(field, analyzer);
	        		     
		      Query query = qparser.parse("intoxicados una vela");
		      
		      System.out.println("Searching for: " + query.toString(field));
		      
		      Hits hits = searcher.search(query);
		      System.out.println(hits.length());
		      
		      for (int start = 0; start < 10 /*hits.length()*/; start ++) 
		      {
		    	  Document d = hits.doc(start);
		    	  System.out.println(d.getField("artist") + " | " + d.getField("album") + " | " + d.getField("track") + " | " + hits.score(start) );
		    	  
		      }		      		      		      		
	}
}

